Event.observe ( window, 'load', bootstrap );

function bootstrap () {
  $( 'milestone_shortcut' ).onchange = function () {
    if ( this.value === '-' ) {
      $( 'milestone_name' ).disabled = '';
      $( 'milestone_kind' ).disabled = '';
      
      alter_commit ();
    }
    else {
      $( 'milestone_name' ).disabled = 'disabled';
      $( 'milestone_kind' ).disabled = 'disabled';
      
      $( 'milestone_kind' ).value = 'Tag';
      
      $( 'subversion_commit' ).disabled = '';
    }
  };
  
  $( 'milestone_name' ).observe ( 'mouseout', function() { alter_commit () } );
}

function alter_commit () {
  var widget = $( 'milestone_name' );
  
  var milestone_name = widget.value;

  var re_revision = /^r\d+$/;

  var re_dr       = get_re_dr ();
  var re_softpack = /^softpack-\d+\.\d+(\.\d+)?$/;
  var re_released = /^[a-z][a-z0-9]+-20[123456789][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$/;

  $( 'subversion_commit' ).disabled = 'disabled';

  if ( widget.value === widget.defaultValue ) {
  }
  else if ( ! re_revision.test ( $( 'svn_product_revision' ).innerHTML ) ) {
    alert ( 'Product was not checked out.' );
  }
  else if ( re_dr.test ( milestone_name ) || re_softpack.test ( milestone_name ) || re_released.test ( milestone_name ) ) {
    $( 'subversion_commit' ).disabled = '';
  }
}
